﻿using Protocol.Code;
using Script.Framework;
using Script.Model;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI._2.Fight
{
    public class BottomPanel : UIBase
    {
        private void Awake()
        {
            Bind(UIEvent.CHANGE_MUTIPLE);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.CHANGE_MUTIPLE:
                    ChangeMutiple((int) message);
                    break;
                default:
                    break;
            }
        }


        private Text txtBean;
        private Text txtMutiple;
        private Button btnChat;
        private Image imgChoose;
        private Button[] btns;

        private SocketMsg socketMsg;

        // Start is called before the first frame update
        private void Start()
        {
            //获取对象
            txtBean = transform.Find("txtBean").GetComponent<Text>();
            txtMutiple = transform.Find("txtMutiple").GetComponent<Text>();
            btnChat = transform.Find("btnChat").GetComponent<Button>();
            imgChoose = transform.Find("imgChoose").GetComponent<Image>();
            btns = new Button[7];

            Transform parent = transform.Find("imgChoose");

            for (int i = 0; i < btns.Length; i++)
            {
                btns[i] = parent.GetChild(i).GetComponent<Button>();
            }


            // 创建对象
            socketMsg = new SocketMsg();

            // 注册事件
            btnChat.onClick.AddListener(SetChooseActive);

            btns[0].onClick.AddListener(ChatClick1);
            btns[1].onClick.AddListener(ChatClick2);
            btns[2].onClick.AddListener(ChatClick3);
            btns[3].onClick.AddListener(ChatClick4);
            btns[4].onClick.AddListener(ChatClick5);
            btns[5].onClick.AddListener(ChatClick6);
            btns[6].onClick.AddListener(ChatClick7);


            // 初始化界面
            imgChoose.gameObject.SetActive(false);
            RefreshPanel(Models.GameModel.UserDto.Bean);
        }


        /// <summary>
        /// 刷新豆子数
        /// </summary>
        /// <param name="beanCount"></param>
        private void RefreshPanel(int beanCount)
        {
            txtBean.text = "X" + beanCount;
        }

        /// <summary>
        /// 刷新倍数
        /// </summary>
        /// <param name="muti"></param>
        private void ChangeMutiple(int muti)
        {
            txtMutiple.text = "倍数 X " + muti;
        }

        /// <summary>
        /// 设置聊天面板的显示
        /// </summary>
        private void SetChooseActive()
        {
            bool active = imgChoose.gameObject.activeInHierarchy;

            //// 设置与当前状态相反的状态
            imgChoose.gameObject.SetActive(!active);
        }


        private void ChatClick1()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 1);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick2()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 2);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick3()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 3);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick4()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 4);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick5()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 5);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick6()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 6);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        private void ChatClick7()
        {
            socketMsg.Change(OpCode.CHAT, ChatCode.CREQ, 7);
            Dispatch(AreaCode.NET, 0, socketMsg);
        }

        override
            public void OnDestroy()
        {
            base.OnDestroy();

            btnChat.onClick.RemoveListener(SetChooseActive);
        }
    }
}